home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Imaging / AkuaEyes next >
Encoding:
Text File  |  1998-11-13  |  7.5 KB  |  310 lines  |  [TEXT/AKu?]

  1. property kasIdleSecs : 1
  2. property kasEyeSize : 66 -- Diameter of eye
  3. property kasIrisSize : round (kasEyeSize / 2) - 1 -- Diameter of iris
  4. property kasPupilSize : round (kasIrisSize / 4) -- Diameter of pupil
  5. property kasRadI : round (kasIrisSize / 2) -- Radius of iris
  6. property kasRadE : round (kasEyeSize / 2) -- Radius of eye
  7.  
  8. property kasPrettyEyes : true -- Use 3-D'ish eyes?
  9.  
  10. property kasColorL : {11111, 33333, 44444} -- Left Iris
  11. property kasColorR : {11111, 22222, 44444} -- Right Iris
  12. property kasColorB : {60000, 59000, 58000} -- Eye Ball
  13. property kasBack : {22222, 22222, 19999}
  14.  
  15. property kasName : "AkuaEyes V1.0"
  16.  
  17. global gasWin -- Window
  18. global gasWinLoc -- Window location
  19. global gasWinW -- Width of window
  20. global gasMidXL, gasMidY, gasMidXR -- Midpoint of eyes in global points
  21. global gasIrisL, gasIrisR -- Local iris bounds
  22. global gasEyeL, gasEyeR -- Local Eyeball bounds
  23. global gasEyePic -- Eye Ball Picture
  24. global gasIrisPicL, gasIrisPicR -- Iris Picture
  25. global gasIrisRgn -- Region of last iris location
  26. global gasEyeRgnL, gasEyeRgnR -- Eyeball regions
  27. global gasMouseLoc -- Last calculated mouse position
  28.  
  29.  
  30. on run
  31.     pfLoad()
  32.     
  33.     set gasWinW to round (kasEyeSize * 2.25)
  34.     set gasEyeL to {0, 0, kasEyeSize, kasEyeSize}
  35.     set gasEyeR to {gasWinW - kasEyeSize, 0, gasWinW, kasEyeSize}
  36.     set gasMouseLoc to {-1111, -1111}
  37.     
  38.     set gasWin to ¬
  39.         display drawing titled kasName ¬
  40.             with dimensions {gasWinW, kasEyeSize} ¬
  41.             located at gasWinLoc
  42.     
  43.     draw a box into gasWin ¬
  44.         inside of {0, 0, gasWinW, kasEyeSize} ¬
  45.         filling it by erasing it ¬
  46.         using state {bg col64:kasBack}
  47.     
  48.     EyeMidPoints(gasWinLoc)
  49.     
  50.     CreateRgns()
  51.     
  52.     if (kasPrettyEyes) then
  53.         CreateEye()
  54.         CreateIris()
  55.     end if
  56. end run
  57.  
  58.  
  59. on quit
  60.     pfSave()
  61.     display drawing gasWin with disposal
  62.     continue quit
  63. end quit
  64.  
  65.  
  66. on idle
  67.     repeat with i from 1 to 60
  68.         if EyeCalc() then
  69.             EyesDraw()
  70.             pause for 5 with break on key and break on button
  71.             set i to 1
  72.         end if
  73.     end repeat
  74.     
  75.     return 1
  76. end idle
  77.  
  78.  
  79. on EyeMidPoints(winLoc)
  80.     set gasWinLoc to winLoc
  81.     
  82.     set eyeHalf to round (kasEyeSize / 2)
  83.     
  84.     set gasMidY to round (item 2 of gasWinLoc) + eyeHalf
  85.     set gasMidXL to (item 1 of gasWinLoc) + eyeHalf
  86.     set gasMidXR to (item 1 of gasWinLoc) + gasWinW - eyeHalf
  87. end EyeMidPoints
  88.  
  89.  
  90. on EyeCalc()
  91.     set mouseLoc to pointer location of (input state)
  92.     
  93.     if (mouseLoc is not gasMouseLoc) then
  94.         set gasMouseLoc to mouseLoc
  95.         set gasIrisL to EyeCalcOne(gasMidXL)
  96.         set gasIrisR to EyeCalcOne(gasMidXR)
  97.         return true
  98.     end if
  99.     
  100.     return false
  101. end EyeCalc
  102.  
  103.  
  104. on EyeCalcOne(xp)
  105.     -- Calculate our mid point by triangulation
  106.     set a2 to (item 1 of gasMouseLoc) - xp
  107.     set b2 to (item 2 of gasMouseLoc) - gasMidY
  108.     -- Distance to mouse (hypothenuse of large triangle)
  109.     set c2 to (a2 ^ 2 + b2 ^ 2) ^ 0.5
  110.     -- Proportion of radius (c1) to c2
  111.     set ratio to kasRadI / c2
  112.     -- Scale a1, b1
  113.     set a1 to round (ratio * a2)
  114.     set b1 to round (ratio * b2)
  115.     -- Create box, convert to local coords
  116.     set x1 to xp + a1 - (item 1 of gasWinLoc)
  117.     set y1 to gasMidY + b1 - (item 2 of gasWinLoc)
  118.     return {x1 - kasRadI, y1 - kasRadI, x1 + kasRadI, y1 + kasRadI}
  119. end EyeCalcOne
  120.  
  121.  
  122. on EyesDraw()
  123.     -- Map Iris region to left & right iris
  124.     set irisRgnL to calculate region from the mapping ¬
  125.         of region gasIrisRgn inside of gasIrisL
  126.     set irisRgnR to calculate region from the mapping ¬
  127.         of region gasIrisRgn inside of gasIrisR
  128.     
  129.     -- Get left & right eye clips by subtracting the irises from the eyeballs
  130.     set eyeRgnL to calculate region from the difference ¬
  131.         of region gasEyeRgnL ¬
  132.         with region irisRgnL
  133.     set eyeRgnR to calculate region from the difference ¬
  134.         of region gasEyeRgnR ¬
  135.         with region irisRgnR
  136.     
  137.     if (kasPrettyEyes) then
  138.         -- Draw Left Iris
  139.         draw a picture into gasWin ¬
  140.             inside of gasIrisL ¬
  141.             using data gasIrisPicL ¬
  142.             without recording
  143.         
  144.         -- Draw Right Iris
  145.         draw a picture into gasWin ¬
  146.             inside of gasIrisR ¬
  147.             using data gasIrisPicR ¬
  148.             without recording
  149.         
  150.         -- Draw Left Eyeball
  151.         draw a picture into gasWin ¬
  152.             inside of gasEyeL ¬
  153.             clipping to region eyeRgnL ¬
  154.             using data gasEyePic ¬
  155.             without recording
  156.         
  157.         -- Draw Right Eyeball
  158.         draw a picture into gasWin ¬
  159.             inside of gasEyeR ¬
  160.             clipping to region eyeRgnR ¬
  161.             using data gasEyePic ¬
  162.             without recording
  163.     else
  164.         -- Draw Left Iris
  165.         draw an oval into gasWin ¬
  166.             inside of gasIrisL ¬
  167.             filling it with the pen ¬
  168.             using state {fg col64:kasColorL} ¬
  169.             without recording
  170.         
  171.         -- Draw Right Iris
  172.         draw an oval into gasWin ¬
  173.             inside of gasIrisR ¬
  174.             filling it with the pen ¬
  175.             using state {fg col64:kasColorR} ¬
  176.             without recording
  177.         
  178.         -- Draw Left Eyeball
  179.         draw an oval into gasWin ¬
  180.             inside of gasEyeL ¬
  181.             filling it with the pen ¬
  182.             using state {fg col64:kasColorB} ¬
  183.             clipping to region eyeRgnL ¬
  184.             without recording
  185.         
  186.         -- Draw Right Eyeball
  187.         draw an oval into gasWin ¬
  188.             inside of gasEyeR ¬
  189.             filling it with the pen ¬
  190.             clipping to region eyeRgnR ¬
  191.             without recording
  192.     end if
  193.     -- Has window moved?
  194.     set wLoc to screen location of ¬
  195.         (display drawing gasWin)
  196.     if (wLoc is not gasWinLoc) then EyeMidPoints(wLoc)
  197. end EyesDraw
  198.  
  199.  
  200. on CreateEye()
  201.     set gasEyePic to CreateBall(kasEyeSize, gasEyeRgnL, kasColorB, 0)
  202. end CreateEye
  203.  
  204.  
  205. on CreateIris()
  206.     set gasIrisPicL to CreateBall(kasIrisSize, gasIrisRgn, kasColorL, kasPupilSize)
  207.     set gasIrisPicR to CreateBall(kasIrisSize, gasIrisRgn, kasColorR, kasPupilSize)
  208. end CreateIris
  209.  
  210.  
  211. on CreateBall(dim, rgn, col, blackCenter)
  212.     set ballWin to display drawing titled ¬
  213.         "Creating Ball" with dimensions {dim, dim}
  214.     
  215.     set box to {0, 0, dim, dim}
  216.     
  217.     set cnt to round (dim / 3)
  218.     
  219.     copy col to fgc
  220.     set colStepR to round ((65000 - (item 1 of fgc)) / cnt)
  221.     set colStepG to round ((65000 - (item 2 of fgc)) / cnt)
  222.     set colStepB to round ((65000 - (item 3 of fgc)) / cnt)
  223.     
  224.     repeat with i from 1 to cnt
  225.         draw an oval into ballWin ¬
  226.             inside of box ¬
  227.             using state {fg col64:fgc, pen size:{2, 2}}
  228.         
  229.         set item 3 of box to (item 3 of box) - 1
  230.         set item 4 of box to (item 4 of box) - 1
  231.         
  232.         draw an oval into ballWin ¬
  233.             inside of box ¬
  234.             using state {fg col64:fgc, pen size:{1, 1}}
  235.         
  236.         set item 1 of box to (item 1 of box) + 1
  237.         set item 2 of box to (item 2 of box) + 1
  238.         set item 3 of box to (item 3 of box) - 1
  239.         set item 4 of box to (item 4 of box) - 1
  240.         
  241.         set item 1 of fgc to (item 1 of fgc) + colStepR
  242.         set item 2 of fgc to (item 2 of fgc) + colStepG
  243.         set item 3 of fgc to (item 3 of fgc) + colStepB
  244.     end repeat
  245.     
  246.     if (blackCenter is not 0) then
  247.         set d to round ((dim - blackCenter) / 2)
  248.         draw an oval into ballWin ¬
  249.             inside of {d, d, d + blackCenter, d + blackCenter} ¬
  250.             using state {fg col64:{0, 0, 0}} ¬
  251.             filling it with the pen
  252.     end if
  253.     
  254.     set ball to ¬
  255.         capture picture from ballWin ¬
  256.             inside region rgn ¬
  257.             using depth 8 ¬
  258.             with pixel conversion and dithering
  259.     
  260.     display drawing ballWin with disposal
  261.     
  262.     return ball
  263. end CreateBall
  264.  
  265.  
  266. on CreateRgns()
  267.     set ballWin to display drawing titled ¬
  268.         "Creating Regions" with dimensions {kasEyeSize, kasEyeSize}
  269.     
  270.     -- Left Eye Region
  271.     set box to {0, 0, kasEyeSize, kasEyeSize}
  272.     
  273.     draw an oval into ballWin ¬
  274.         inside of box
  275.     
  276.     set gasEyeRgnL to capture region from ballWin
  277.     
  278.     -- Basic iris region
  279.     set box to {0, 0, kasIrisSize, kasIrisSize}
  280.     
  281.     draw an oval into ballWin ¬
  282.         inside of box ¬
  283.         with a clear slate
  284.     
  285.     set gasIrisRgn to capture region from ballWin
  286.     
  287.     -- Map left eye region to right eye
  288.     set gasEyeRgnR to calculate region from the mapping ¬
  289.         of region gasEyeRgnL ¬
  290.         inside of gasEyeR
  291.     
  292.     display drawing ballWin with disposal
  293. end CreateRgns
  294.  
  295.  
  296. on pfLoad()
  297.     try
  298.         set myPref to load preference named kasName
  299.     on error
  300.         set myPref to {{-1, -1}}
  301.     end try
  302.     
  303.     set gasWinLoc to item 1 of myPref
  304. end pfLoad
  305.  
  306.  
  307. on pfSave()
  308.     save preference {gasWinLoc} named kasName
  309. end pfSave
  310.